/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yang
 * Date: 2022-09-13
 * Time: 9:23
 */
class Counter{
    public int num = 0;
    public String locker1 = "locker";
    public String locker2 = "locker2";
    public synchronized void increase(){
        num++;
    }

    public void func1(){
        synchronized(this){
            while (true){
                System.out.println("我是函数一");
            }
        }

    }

    public void func2(){
        synchronized(this){
            while (true){
                System.out.println("我是函数二");
            }
        }
    }
}

public class ThreadCounter {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000000; i++) {
                counter.increase();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000000; i++) {
                counter.increase();
            }
        });
        long start = System.currentTimeMillis();
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long end = System.currentTimeMillis();


        System.out.println(counter.num);
        System.out.println("运行了" + (end - start) + "ms");


        Counter counter2 = new Counter();
        start = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            counter2.increase();
        }
        end = System.currentTimeMillis();
        System.out.println(counter2.num);
        System.out.println("运行了" + (end - start) + "ms");

//        Counter counter = new Counter();
//        Thread t1 = new Thread(() -> {
//            for (int i = 0; i < 5000000; i++) {
//                counter.func1();
//            }
//        });
//        Thread t2 = new Thread(() -> {
//            for (int i = 0; i < 5000000; i++) {
//                counter.func1();
//            }
//        });
//
//        t1.start();
//        t2.start();

    }
}

//public class ThreadCounter {
//    // 多线程并不一定就能提高速度，可以观察，count 不同，实际的运行效果也是不同的
//    private static final long count = 10_0000_0000;
//    public static void main(String[] args) throws InterruptedException {
//// 使用并发方式
//        concurrency();
//// 使用串行方式
//        serial();
//    }
//    private static void concurrency() throws InterruptedException {
//        long begin = System.nanoTime();
//// 利用一个线程计算 a 的值
//        Thread thread = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                int a = 0;
//                for (long i = 0; i < count; i++) {
//                    a--;
//                }
//            }
//        });
//        thread.start();
//// 主线程内计算 b 的值
//        int b = 0;
//        for (long i = 0; i < count; i++) {
//            b--;
//        }
//// 等待 thread 线程运行结束
//        thread.join();
//// 统计耗时
//        long end = System.nanoTime();
//        double ms = (end - begin) * 1.0 / 1000 / 1000;
//        System.out.printf("并发: %f 毫秒%n", ms);
//    }
//    private static void serial() {
//// 全部在主线程内计算 a、b 的值
//        long begin = System.nanoTime();
//        int a = 0;
//        for (long i = 0; i < count; i++) {
//            a--;
//        }
//        int b = 0;
//        for (long i = 0; i < count; i++) {
//            b--;
//        }
//        long end = System.nanoTime();
//        double ms = (end - begin) * 1.0 / 1000 / 1000;
//        System.out.printf("串行: %f 毫秒%n", ms);
//    }
//}
